package com.sita.linboard.login;

import android.annotation.TargetApi;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.readystatesoftware.systembartint.SystemBarTintManager;
import com.sita.linboard.R;
import com.sita.linboard.base.BaseActivity;
import com.sita.linboard.driverInfo.VerificationCodeBean;
import com.sita.linboard.http.RestClient;
import com.sita.linboard.person.SettingActivity;
import com.sita.linboard.utils.ToastUtils;

import retrofit.Callback;
import retrofit.RetrofitError;
import retrofit.client.Response;

/**
 * Created by Jason on 2016/9/26.
 */
public class ForgetPassActivity extends BaseActivity implements View.OnClickListener{
    private EditText phone,verification,newPass,confirmPass;
    private Button confirmBtn;
    private TextView countdown;
    private static int JUMPTYPE;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setSystemBar(R.color.main_title_bg);
        Intent intent = getIntent();
        JUMPTYPE = intent.getIntExtra("JumbWhere",0);
    }

    @Override
    protected void initData() {
        confirmBtn.setOnClickListener(this);
        countdown.setOnClickListener(this);

    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        phone = bindView(R.id.driver_forget_phone);
        verification = bindView(R.id.driver_verification_phone);
        newPass= bindView(R.id.driver_newpass_phone);
        confirmPass = bindView(R.id.driver_confirmnewpass_phone);
        confirmBtn = bindView(R.id.driver_forget_confirm);
        countdown = bindView(R.id.forget_countdown);
    }

    @Override
    protected int getId() {
        return R.layout.activity_forgetpass;
    }
    public static void ForgetPassIntent(Context context,int type){
        Intent intent = new Intent(context,ForgetPassActivity.class);
        intent.putExtra("JumbWhere",type);
        context.startActivity(intent);
    }
    public  void setSystemBar(int color){
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            setTranslucentStatus(true);
        }

        SystemBarTintManager tintManager = new SystemBarTintManager(this);
        tintManager.setStatusBarTintEnabled(true);
        tintManager.setStatusBarTintResource(color);//通知栏所需颜色

    }

    @TargetApi(19)
    private  void setTranslucentStatus(boolean on) {
        Window win = getWindow();
        WindowManager.LayoutParams winParams = win.getAttributes();
        final int bits = WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS;
        if (on) {
            winParams.flags |= bits;
        } else {
            winParams.flags &= ~bits;
        }
        win.setAttributes(winParams);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.forget_countdown:
                //获取验证码
                if (phone.getText().length() == 0) {
                    ToastUtils.show("请输入手机号", 3);
                } else {
                    RestClient.getRestService().getVerificationCode(phone.getText().toString(), 0, new Callback<VerificationCodeBean>() {
                        @Override
                        public void success(VerificationCodeBean verificationCodeBean, Response response) {

                        }

                        @Override
                        public void failure(RetrofitError error) {

                        }
                    });
                }
                break;
            case R.id.driver_forget_confirm:
                //确认修改
                ChangeThePassword();

                break;
        }
    }
    private void ChangeThePassword(){
       if (phone.getText().length()==0
               ||verification.getText().length()==0
               ||newPass.getText().length()==0
               ||confirmPass.getText().length()==0){
           ToastUtils.show("输入信息有误",3);
           return;
       }else {
           if (phone.getText().length()>0){
               if (BaseActivity.isPhoneNumberValid(phone.getText().toString())){
//                   if (verification.getText().toString().equals("265307")){
                       if (newPass.getText().toString().equals(confirmPass.getText().toString())){
                           ForgetPassRequest request = new ForgetPassRequest();
                           request.mobile = phone.getText().toString();
                           request.password = newPass.getText().toString();
//                           request.smscode = "265307";
                           request.smscode = verification.getText().toString();
                           RestClient.getRestService().forgetPassword(request, new Callback<ForgetPassBackBean>() {
                               @Override
                               public void success(ForgetPassBackBean forgetPassBackBean, Response response) {
                                   if (forgetPassBackBean.getErrorCode().equals("0")&&forgetPassBackBean.getData()!=null){
                                       if (JUMPTYPE==1){
                                           ToastUtils.show("密码更新成功，请重新登录",2);
                                           DriverLoginActivity.DriverLoginIntent(ForgetPassActivity.this);
                                       }else if (JUMPTYPE==2){
                                           ToastUtils.show("密码更新成功",2);
                                           SettingActivity.SettingIntent(ForgetPassActivity.this);
                                       }
                                       finish();
                                   }
                               }

                               @Override
                               public void failure(RetrofitError error) {
                                 ToastUtils.show("密码更新失败，请检查输入信息",2);
                               }
                           });
                       }
                       else {
                           ToastUtils.show("验证密码出错",2);
                       }
//                   }
//                   else {
//                       ToastUtils.show("验证码输入有误，请重新输入!",3);
//                   }
               }else {
                   ToastUtils.show("手机号输入有误，请重新输入！",3);
                   return;
               }
           }
       }

    }

}
